﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenWings.Entities;
using OpenWings.UI.Helpers;
using OpenWings.UI.Elements;
using System.Diagnostics;
using System.Reflection;
using Microsoft.Win32;
using OpenWings.Engine;

namespace OpenWings.UI.Controls
{
    public partial class ClientsView : UserControl
    {
        public event EventHandler<ClientEventArgs> LaunchClient;
        public event EventHandler<ClientEventArgs> SelectClient;
        public event EventHandler<ClientEventArgs> EditClient;
        public event EventHandler<ClientEventArgs> AddClient;
        public event EventHandler<ClientEventArgs> DeleteClient;
        public event EventHandler<ClientsEventArgs> DetectedClients;

        List<Client> _Clients;
        Client _SelectedClient;
        Server _LaunchWithServer;

        public ClientsView()
        {
            InitializeComponent();
        }

        public void LaunchWithServer(Server server)
        {
            _LaunchWithServer = server;
        }

        public List<Client> Clients
        {
            get
            {
                return _Clients;
            }
            set
            {
                _Clients = value;
                PopulateListView(_Clients);
                SelectedClient = null;
            }
        }

        public Client SelectedClient
        {
            get
            {
                return _SelectedClient;
            }
            set
            {
                _SelectedClient = value;

#if DEBUG
                Trace.WriteLine(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name);
#endif

                if (SelectClient != null)
                    SelectClient(this, new ClientEventArgs(_SelectedClient));
            }
        }

        void PopulateListView(List<Client> clients)
        {
            ListHelper.PopulateListView(ClientsListView, delegate(ListView view)
            {
                view.Items.Clear();

                if (clients != null)
                    foreach (Client OneClient in clients)
                        view.Items.Add(new ListViewItemClient(OneClient));
            });
        }

        void ClientsListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ListViewItemClient ItemClient = e.Item as ListViewItemClient;

            if (e.IsSelected && ItemClient != null)
                SelectedClient = ItemClient.InnerEntity;
            else
                SelectedClient = null;
        }

        void ClientsListView_ItemActivate(object sender, EventArgs e)
        {
            if (SelectedClient == null)
                return;

            if (LaunchClient != null)
                LaunchClient(this, new ClientEventArgs(SelectedClient));
        }

        private void PropertiesMenuItem_Click(object sender, EventArgs e)
        {
            if (EditClient != null)
                EditClient(this, new ClientEventArgs(SelectedClient));
        }

        private void AddMenuItem_Click(object sender, EventArgs e)
        {
            Client NewClient = new Client();
            NewClient.Name = "New client";

            ListViewItem NewItem = new ListViewItemClient(NewClient);
            ClientsListView.Items.Add(NewItem);

            NewItem.Selected = true;

            if (AddClient != null)
                AddClient(this, new ClientEventArgs(NewClient));
        }

        private void ClientContextMenu_Opening(object sender, CancelEventArgs e)
        {
            DeleteMenuItem.Text = "&Delete";

            if (SelectedClient == null)
            {
                if (_LaunchWithServer == null)
                    LaunchMenuItem.Text = "Please select a server and a client";
                else
                    LaunchMenuItem.Text = "Please select a client for " + _LaunchWithServer.Name;

                DeleteMenuItem.Enabled = false;
                PropertiesMenuItem.Enabled = false;
            }
            else
            {
                if (_LaunchWithServer == null)
                    LaunchMenuItem.Text = "Please select a server to launch with " + SelectedClient.Name;
                else
                    LaunchMenuItem.Text = "&Launch " + SelectedClient.Name + " on " + _LaunchWithServer.Name;
                
                DeleteMenuItem.Text += " " + SelectedClient.Name;
                
                DeleteMenuItem.Enabled = true;
                PropertiesMenuItem.Enabled = true;
            }
        }

        private void DeleteMenuItem_Click(object sender, EventArgs e)
        {
            if (SelectedClient == null)
                return;

            if (MessageBox.Show(this, "Are you sure you want to delete " + SelectedClient.Name + " ?", "Delete client", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                if (DeleteClient != null)
                    DeleteClient(this, new ClientEventArgs(SelectedClient));

                ClientsListView.Items.Remove(ClientsListView.SelectedItems[0]);
            }
        }

        private void AutoDetectMenuItem_Click(object sender, EventArgs e)
        {
            if (DetectedClients != null)
                DetectedClients(this, new ClientsEventArgs(AutoDetection.AutoDetect()));
        }
    }

    public class ClientsEventArgs : EventArgs
    {
        public List<Client> Clients;

        public ClientsEventArgs(List<Client> clients)
        {
            Clients = clients;
        }
    }

    public class ClientEventArgs : EventArgs
    {
        public Client Client;

        public ClientEventArgs(Client client)
        {
            Client = client;
        }
    }


}
